package siyuan.run.nacos.security.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import siyuan.run.nacos.security.model.User;
import siyuan.run.nacos.security.service.UserService;
import siyuan.run.nacos.security.util.Result;

import java.util.List;

/**
 * @program: sy
 * @description:
 * @author: SY
 * @create: 2020-12-20 21:53
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping()
    public List<User> get() {
        return userService.findAll();
    }

    @PostMapping("register")
    public ResponseEntity<Result> register (@RequestBody User user){
        Result result = new Result();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePwd = encoder.encode(user.getPassword());
        user.setPassword(encodePwd);
        result.setObject(userService.save(user));
        return ResponseEntity.ok(result);
    }

}
